﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECMS.AppService.Interfaces;
using ECMS.AppService.Model.Models;
using ECMS.AppService.Model.Requests.AppUser;
using ECMS.AppService.Model.Responses;
using ECMS.Infrastructure.ClientData;
using ECMS.Infrastructure.Helper;
using ECMS.Infrastructure.Messaging;
using ECMS.Manage.Models;
using Microsoft.AspNetCore.Mvc;

namespace ECMS.Manage.Controllers
{
    public class AppUserController : Controller
    {
        private readonly IAppUserService _service;
        private RouteModel _routeModel;
        public AppUserController(IAppUserService service)
        {
            _service = service;
            _routeModel = new RouteModel
            {
                Controller = "AppUser",
             
            };
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult CreateOrEdit()
        {
            ViewBag.Route = _routeModel;
            return View(new SysUserModel());
        }
        public async Task<IActionResult> Edit(Guid? guid)
        {
            ViewBag.Route = _routeModel;

            if (guid == null)
            {
                return View(_routeModel.Action, new AppUserModel());
            }
            var single = await _service.GetDetailAsync(guid.Value);

            return View(_routeModel.Action, single);
        }
        public IActionResult List()
        {
            ViewBag.Route = _routeModel;
            return View();
        }

        #region Hander
        // [ValidateAntiForgeryToken]
        [HttpPost]
        public async Task<ApiResult<SaveResponse>> Save([FromBody]AppUserModel model)
        {
            return await _service.SaveAsync(model);
        }
        [HttpPost]
        public async Task<dynamic> GetPages([FromBody]GetAppUserPagesRequest request)
        {
            var page = await _service.GetPagesAsync(request);

            return ObjectHelper.PageToLayuiTable(page);
        }
        [HttpPost]
        public async Task<ApiResult<string>> Delete([FromBody]GuidRequest request)
        {
            return await _service.DeleteAsync(request.Guid);
        }
        #endregion
    }
}